package com.daxt.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.daxt.base.common.util.RedisUtil;
import com.daxt.mapper.warehouse.WarehouseMapper;
import com.daxt.mapper.warehouse.WarehouseShelfMapper;
import com.daxt.mapper.warehouse.WarehouseShelfPostionMapper;
import com.daxt.model.service.warehouse.vo.Warehouse;
import com.daxt.model.service.warehouse.vo.WarehouseShelf;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostion;
import com.daxt.service.SystemService;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.sys.SystemConfigMapper;
import com.daxt.model.service.base.vo.SystemConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "系统配置")
@RestController
public class SystemConfigController {
	
	@Autowired
	private SystemConfigMapper systemConfigMapper;
	@Autowired
	private SystemService systemService;


	@ApiOperation(value = "获取类型列表", notes = "")
	@GetMapping("/SystemConfig") 
	public Result<List<SystemConfig>> list(@ApiParam("标识（user用户，system系统）") @RequestParam(value = "flag", required = false) String flag){
		QueryWrapper<SystemConfig> queryWrapper=new QueryWrapper<SystemConfig>();
		if(!StringUtils.isEmpty(flag)) queryWrapper.eq("flag",flag);
		List<SystemConfig> list = systemConfigMapper.selectList(queryWrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, list);
	}
	
	@ApiOperation(value = "获取类型列表", notes = "") 
	@GetMapping("/SystemConfig/{key}") 
	public Result<SystemConfig> queryKey(@PathVariable("key") String sysKey){
		QueryWrapper<SystemConfig> queryWrapper=new QueryWrapper<SystemConfig>();
		queryWrapper.eq("sysKey", sysKey);
		List<SystemConfig> list = systemConfigMapper.selectList(queryWrapper);
		SystemConfig systemConfig=new SystemConfig();
		if(list!=null&&list.size()>0) {
			systemConfig = list.get(0);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, systemConfig);
	}
	
	
//	@ApiOperation(value = "设置类型", notes = "")
//	@PutMapping("/systemConfig/{sysKey}/{sysValue}")
//	public Result<SystemConfig> setSystemConfig(@PathVariable("sysKey") String sysKey,@PathVariable("sysValue")String sysValue){
//		UpdateWrapper<SystemConfig> updateWrapper=new UpdateWrapper<SystemConfig>();
//		 updateWrapper.set("sysValue", sysValue);
//		 updateWrapper.eq("sysKey",sysKey);
//		int update = systemConfigMapper.update(null, updateWrapper);
//
//		RedisUtil.setSysConfig(sysKey,sysValue);
//
//		return ResultUtil.data(update>0?ResultCode.RESULT_SUCCESS.getCode():ResultCode.RESULT_ERROR.getCode(), null, null);
//	}


	@ApiOperation(value = "修改", notes = "")
	@PutMapping("/systemConfig")
	@Transactional(rollbackFor = Exception.class)
	public Result<String> edit(@RequestBody SystemConfig entity) {
		try {
			entity.setSysKey(null);//键不修改
			SystemConfig baseUser = new SystemConfig();
			BeanUtils.copyProperties(entity, baseUser);

			int rows = systemConfigMapper.updateById(baseUser);
			if(rows > 0){
				SystemConfig bean = systemConfigMapper.selectById(baseUser.getId());
				RedisUtil.setSysConfig(bean.getSysKey(),bean.getSysValue());
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
			}else{
				return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "修改0条数据！！！", null);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "操作异常！！！", null);
		}
	}




	@ApiOperation(value = "新增", notes = "")
	@PostMapping("/systemConfig/save")
	public Result<String> save(@RequestBody SystemConfig entity){

		if(systemService.soleKey(entity.getSysKey())){
			systemConfigMapper.insert(entity);
			RedisUtil.setSysConfig(entity.getSysKey(),entity.getSysValue());
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
		}
		return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "数据唯一性校验", null);

	}



}
